#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include "../head/default.h"         //define 宏定义及static变量                                      
#include "../head/log.h"         //define 宏定义及static变量
void f_logging(enum LogLevel level, const char *fmt, ...);

int f_read_conf(char conf_file_path[], char *conf_name, char res_var[]) {
    char readbuf[VAR_MAX_LEN] = {0};
    char findbuf[VAR_MAX_LEN] = {0};
    memset(res_var, 0x00, sizeof(res_var));
    strcpy(findbuf, conf_name);
    int line = 0;
    FILE *fp = fopen(conf_file_path, "r");
    if(fp == NULL) {
        char log_info[VAR_MAX_LEN];
        memset(log_info, 0x00, sizeof(log_info));
        sprintf(log_info, "%s %s", conf_file_path, "配置文件不存在!");
        f_logging(ERROR, log_info);
        exit(1);
    }

    while(fgets(readbuf,sizeof(readbuf),fp)) { //逐行循环读取文件，直到文件结束
        line++;
        if(!strncmp(readbuf,"#",1) || !strncmp(readbuf,"\n",1)) { //忽略注释(#)和空行
            continue;
        }

        if(strstr(readbuf, findbuf)) {    //查找配置文件名
            char *p = strchr(readbuf,'=');  //确定“=”位置
            do {
                p += 1;
            } while(*p == ' ');
            sprintf(res_var,"%s",p);
            fclose(fp);
            return 0;
        }
    }
    return -1;
}
